package com.jdjc.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.jdjc.auth.application.convert.AuthUserDTOConverter;
import com.jdjc.auth.domin.entity.AuthUserBO;
import com.jdjc.auth.domin.service.AuthUserDominService;
import com.jdjc.auth.entity.AuthUserDTO;
import com.jdjc.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户controller
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private AuthUserDominService authUserDominService;

    /**
     * 注册用户
     *
     * @param authUserDTO
     * @return
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" UserController：register:dto:{}", authUserDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            /*Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名称不能为空");
            Preconditions.checkNotNull(authUserDTO.getPassword(), "用户密码不能为空");
            Preconditions.checkNotNull(authUserDTO.getEmail(), "用户邮箱不能为空");*/
            //自定义类型装换
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            if (log.isInfoEnabled()) {
                log.info(" UserController：register:bo:{}", authUserBO);
            }
            //注册
            Boolean result = authUserDominService.register(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController:registor:error:{}", e.getMessage(), e);
            return Result.fail("用户注册失败");
        }

    }

    /**
     * 更新用户信息
     *
     * @param authUserDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" UserController：update:dto:{}", authUserDTO);
            }
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            if (log.isInfoEnabled()) {
                log.info(" UserController：update:bo:{}", authUserBO);
            }
            //更新
            Boolean result = authUserDominService.update(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController：update:error:{}", e.getMessage(), e);
            return Result.fail("用户信息更新失败");
        }
    }

    /**
     * 逻辑删除用户
     *
     * @param authUserDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" UserController：delete:dto:{}", authUserDTO);
            }

            //自定义类型装换
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            if (log.isInfoEnabled()) {
                log.info(" UserController：delete:bo:{}", authUserBO);
            }
            //逻辑删除
            Boolean result = authUserDominService.delete(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController：delete:error:{}", e.getMessage(), e);
            return Result.fail("用户删除失败");
        }
    }

    /**
     * 用户的启用与禁用
     *
     * @param authUserDTO
     * @return
     */
    @PostMapping("//changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" UserController：changeStatus:dto:{}", authUserDTO);
            }
            //自定义类型装换
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            if (log.isInfoEnabled()) {
                log.info(" UserController：changeStatus:bo:{}", authUserBO);
            }
            //更新
            Boolean result = authUserDominService.changeStatus(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController：changeStatus:error:{}", e.getMessage(), e);
            return Result.fail("用户启用禁用失败");
        }
    }

    /**
     * 查询用户信息
     *
     * @param authUserDTO
     * @return
     */
    @PostMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" UserController：getUserInfo:dto:{}", authUserDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");

            //自定义类型装换
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            if (log.isInfoEnabled()) {
                log.info(" UserController：getUserInfo:bo:{}", authUserBO);
            }
            AuthUserBO getAuthUserBO = authUserDominService.getUserInfo(authUserBO);

            AuthUserDTO result = AuthUserDTOConverter.INSTANCE.convertBOToDTO(getAuthUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController：getUserInfo:error:{}", e.getMessage(), e);
            return Result.fail("用户信息查看失败");
        }
    }


    /**
     * 用户登录
     * @param validCode
     * @return
     */
    @RequestMapping("/doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            log.info("UserController：doLogin:validCode:{}", validCode);
            //断言校验验证码不能为空
            //Preconditions.checkArgument(!StringUtils.isEmpty(validCode), "验证码不能为空");
            // 获取当前登录会话的令牌信息
            // SaTokenInfo 类包含了令牌的相关信息，如令牌值、创建时间和过期时间
            SaTokenInfo tokenInfo = authUserDominService.doLogin(validCode);
            return Result.ok(tokenInfo);
        } catch (Exception e) {
            log.error("UserController：doLogin:error:{}", e.getMessage(), e);
            return Result.fail("用户登录失败");
        }

    }

    /**
     * 用户退出登录
     * @param userName
     * @return
     */
    @RequestMapping("/logOut")
    public Result doLogOut(@RequestParam String userName) {
        try {
            log.info("UserController：doLogOut:userName:{}", userName);
            //断言校验
            //Preconditions.checkArgument(!StringUtils.isEmpty(userName), "用户名不能为空");

            StpUtil.logout(userName);
            return Result.ok();
        } catch (Exception e) {
            log.error("UserController：doLogOut:error:{}", e.getMessage(), e);
            return Result.fail("用户退出登录失败");
        }
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
